package wili.attrMapping._user.b1.exp;

import java.util.function.Predicate;

import wili.c.reg.MatchRet;

/**
 * 
 * @author weila 2022年10月5日
 */
public class Valueb1 extends VarOrValueb1 {
	public String toString() {
		if (expSt != null)
			return this.expSt;
		return this.getString(1);
	}

	public Valueb1(MatchRet mat) {
		this.expSt = mat.s0.substring(mat.from, mat.to);
		String st = mat.getString(1);
		if (st.charAt(0) == '\'')
			this.strValue = st.substring(1, st.length() - 1);
		else
			this.strValue = st;
	}

	public Valueb1(boolean is) {
		this.strValue = Boolean.valueOf(is).toString();
	}

	public void traverse(Predicate<MathExpB1> pred) {
		pred.test(this);
	}

	@Override
	public Boolean isTrue() {
		return (Boolean) value();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T value() {
		Object ret = this.strValue;
		String st = ret.toString();
		if (st.equals("true"))
			return (T) Boolean.valueOf(!this.isNot);
		if (st.equals("false") || st.equals("undefined") || st.equals("null"))
			return (T) Boolean.valueOf(this.isNot);
		return (T) ret;
	}

	public String getString(int refresh) {
		if (this.expSt != null)
			return this.expSt;
		if (this.strValue != null)
			return this.strValue;
		if (this.isNot)
			return "!" + this.name;

		return this.name;
	}

	public boolean equals(Object o) {
		if (o instanceof Valueb1) {
			Valueb1 obj = (Valueb1) o;
			if (obj.strValue.equals(this.strValue)) {
				if (obj.isNot == this.isNot)
					return true;
			}
		}
		return false;
	}
}
